package com.example.springrabbitmq.config;

import com.example.springrabbitmq.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class DLXConfig {
    // 死信队列
    @Bean("dlxQueue")
    public Queue dlxQueue() {
        return QueueBuilder.durable(Constants.DLX_QUEUE).build();
    }

    @Bean("dlxExchange")
    public DirectExchange dlxExchange() {
        return ExchangeBuilder.directExchange(Constants.DLX_EXCHANGE).durable(true).build();
    }

    @Bean("dlxBinding")
    public Binding dlxBinding(@Qualifier("dlxExchange")DirectExchange exchange, @Qualifier("dlxQueue")Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx");
    }

    // 普通队列(绑定死信队列)
    @Bean("normalQueue")
    public Queue normalQueue() {
        return QueueBuilder.
                durable(Constants.NORMAL_QUEUE).
                deadLetterExchange(Constants.DLX_EXCHANGE).
                deadLetterRoutingKey("dlx").
                ttl(10000).
                maxLength(10L).
                build();
    }

    @Bean("normalExchange")
    public DirectExchange normalExchange() {
        return ExchangeBuilder.directExchange(Constants.NORMAL_EXCHANGE).durable(true).build();
    }

    @Bean("normalBinding")
    public Binding normalBinding(@Qualifier("normalExchange")DirectExchange exchange, @Qualifier("normalQueue")Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("normal");
    }
}
